Refactoring

So, I posted that gif of the title screen on reddit the other day. A lot of good discussion going on. Something came up thanks to u/Archimagus that caused me to refactor the code that moves my camera.

Previously, it looked something like this.

if (currentDirection.x == 1){

if (transform.position.x < targetPositions[x].x){

Move camera code

}

}

This went on for direction being -1, and for y and z as well. All in all, to move the camera was 122 lines of code.

After Archimagus reminded me of some basic functionality, it’s now looks more like this:

if (Vector3.Distance(transform.position, targetPositions[x]) > threshold){

Move camera code

}

And that’s it.

18 lines.

104 lines of garbage code eradicated. Felt pretty good. I also use Mathf.SmoothDamp now for determining the movement, and it makes it a lot smoother.

Needless to say it makes it obvious both how new to Unity I am, and how rusty I am at programming that I forgot about calculating the distance between two Vector3’s.

Leave a Reply

Your email address will not be published. Required fields are marked *